<template>
    <view>
        <u-navbar :is-back="true" title="关于我们" :border-bottom="false"></u-navbar>
        <view class="content">
            <u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
                <u-form-item  label-width="120" label-position="left" label="昵称" prop="name">
                    <u-input :border="false" placeholder="请输入昵称" v-model="model.name" type="text"></u-input>
                </u-form-item>
                <u-form-item label-position="left" label="上传图片" prop="photo" label-width="150">
                    <u-upload :action="action" :header="header" ref="uUpload" name="file" @on-success="uploadSuc" :max-size="1024 * 1024" max-count="1" width="160" height="160"></u-upload>
                </u-form-item>

                <u-button @click="submit" type="primary" class="submit-btn">提交</u-button>
            </u-form>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            let that = this;
            return {
                model: {
                    name: '',
                    photo: '',
                },
                errorType: ['message'],
                action: 'http://loc.fastadmin.com:8201/api.php/common/upload',
                header:{},
                lists: [],
                rules: {
                    name: [
                        {
                            required: true,
                            message: '请输入昵称',
                            trigger: 'blur' ,
                        }
                    ]

                }
            }
        },
        onReady() {
            this.$refs.uForm.setRules(this.rules);
            this.header = {
                token : uni.getStorageSync('token')
            };
            this.lists = this.$refs.uUpload.lists;
        },
        methods: {
            submit() {
                this.$refs.uForm.validate(valid => {
                    if (valid) {
                        let postDate = {};
                        if(this.lists.length ===0 || this.lists[0].response===undefined){
                           // 没有更改头像
                            postDate = {
                                'nickname' : this.model.name
                            }
                        }else{
                            postDate = {
                                'nickname' : this.model.name,
                                'avatar' : this.lists[0].response.data.url
                            }
                        }
                        // 更改用户信息
                        this.$u.api.userProfile(postDate).then(r=>{
                            // 更改成功后重新获取用户信息
                            this.$u.api.getUserInfo().then(res =>{
                                let userinfo = res.data.userinfo;
                                // 同步存储
                                uni.setStorageSync('userinfo', JSON.stringify(userinfo));
                                // 注册成功跳转到个人中心
                                this.$u.route({
                                    type: 'tab',
                                    url: 'pages/template/wxCenter/index'
                                });
                            })
                        });
                    }
                });
            },
            uploadSuc(data, index, lists, name){
                this.$u.toast(data.msg)
            }

        }


    }
</script>

<style lang="scss" scoped>
.content{
    padding: 0 30rpx;
    .submit-btn{
        margin-top: 10rpx;
    }
}

</style>
